﻿using Microsoft.WindowsAzure;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.Diagnostics.Management;

namespace BlobStroage
{
    public partial class CloudPerformanceCounter : Form
    {

        QueryExecuter queryExecuter;

        //Modify based on the information from the windows.azure.com portal
        string storageName = Constants.STORAGE_NAME;
        string privatekey = Constants.PRIVATE_KEY;
        //string counterSpecifier = Constants.COUNTER_SPECIFIER;
        string deploymentId = Constants.DEPLOYMENTID;
        string roleName = Constants.ROLE_NAME;
        string roleInstance = Constants.ROLE_INSTANCE;

        public CloudPerformanceCounter()
        {
            InitializeComponent();
            BindDropdownlist();
            queryExecuter = new QueryExecuter(storageName, privatekey);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //AddPerformance();
        }

        //public void AddPerformance()
        {
            string storageAccoutName = Constants.STORAGE_NAME;
            string privateKey = Constants.PRIVATE_KEY;
            string deploymentId = Constants.DEPLOYMENTID;
            var storageAccount = CloudStorageAccount.Parse(String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", storageAccoutName, privateKey));

            // Get the diagnostics manager for the entire storage account.
            var diagnosticManager = storageAccount.CreateDeploymentDiagnosticManager(deploymentId);

            // Get diagnostics manager for the specific role instance.
            RoleInstanceDiagnosticManager roleDiagManager = diagnosticManager.GetRoleInstanceDiagnosticManager(Constants.ROLE_NAME, Constants.ROLE_INSTANCE);

            //Modify current configuration
            var currentConfiguariton = roleDiagManager.GetCurrentConfiguration();

            //clear performance datasource 
            currentConfiguariton.PerformanceCounters.DataSources.Clear();

            //currentConfiguariton.PerformanceCounters.DataSources.Clear();
            currentConfiguariton.PerformanceCounters.ScheduledTransferPeriod = TimeSpan.FromMinutes(1);

            //currentConfiguariton.PerformanceCounters.DataSources.Add(new PerformanceCounterConfiguration()
            //{
            //    CounterSpecifier = @"\Processor(_Total)\% Processor Time",
            //    SampleRate = TimeSpan.FromSeconds(1)
            //});

            List<string> counters = new List<string>();
            counters.Add(@"\Processor(_Total)\% Processor Time");
            counters.Add(@"\Memory\Available Mbytes");
            counters.Add(@"\TCPv4\Connections Established");
            counters.Add(@"\ASP.NET Applications(__Total__)\Requests/Sec");
            counters.Add(@"\Network Interface(*)\Bytes Received/sec");
            counters.Add(@"\Network Interface(*)\Bytes Sent/sec");

            foreach (string counter in counters)
            {
                PerformanceCounterConfiguration counterConfig = new PerformanceCounterConfiguration();
                counterConfig.CounterSpecifier = counter;
                counterConfig.SampleRate = TimeSpan.FromMinutes(5);
                currentConfiguariton.PerformanceCounters.DataSources.Add(counterConfig);
            }

            //Commit the changes
            roleDiagManager.SetCurrentConfiguration(currentConfiguariton);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            String specifierKey = comboBox1.SelectedValue.ToString();     
            List<PerformanceData> list = queryExecuter.QueryPerformanceCounter(specifierKey, deploymentId, roleName, roleInstance, DateTime.UtcNow.AddMinutes(-10), DateTime.UtcNow);
        }

        public void BindDropdownlist()
        {
            comboBox1.DisplayMember = "SpecifierKey";
            comboBox1.ValueMember = "SpecifierValue";
            comboBox1.DataSource = MonitoredCounter.CounterCollection;
        }
    }
}
